<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" lang="zh" xmlns:th="http://www.thymeleaf.org">
<body class="list">
<div th:replace="include/head.html::html"></div>
<div class="pear-container">
    <div class="layui-card">
        <!--查询from 表单-->

            <div class="layui-card-body">
                <form class="layui-form" >
                    <div class="layui-form-item">
                        <div class="layui-inline">
                        <label class="layui-form-label">用户名</label>
                        <div class="layui-input-inline">
                            <input type="text" name="roleName" placeholder="" class="layui-input">
                        </div>
                        <div class="layui-input-inline">
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="search">
                            <i class="layui-icon layui-icon-search"></i>
                            查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md">
                            <i class="layui-icon layui-icon-refresh"></i>
                            重置
                        </button>
                        </div>
                        </div>
                    </div>
                </form>
            </div>
            <!--<div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="roleName" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>-->
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
        </div>
        <!--表格中的数据 并不显示-->
        <div class="layui-card-body">
            <!--表格外部上侧的按钮-->
            <script type="text/html" id="user-toolbar">
                <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                    <i class="layui-icon layui-icon-add-1"></i>
                    新增
                </button>
            </script>
            <!--<div style="padding-bottom: 10px;" class="tool-bar">
                &lt;!&ndash;<button class="layui-btn layuiadmin-btn-useradmin" data-type="add" shiro:hasPermission="sysRole:add">
                    添加
                </button>&ndash;&gt;
                <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
                    <i class="layui-icon layui-icon-add-1"></i>
                    新增
                </button>
            </div>-->
            <!--数据表格 -->
            <table id="table" lay-filter="table" class="layui-table"></table>
            <!--表格每行的 的操作按钮 -->
            <script type="text/html" id="toolbar">
                {{#  if(d.oid != '00000000000000000000000000000000'){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit" shiro:hasPermission="sysRole:edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete" shiro:hasPermission="sysRole:delBean"></i>删除</a>
                {{#  } else { }}
                <span style="color: red">默认用户不允许编辑</span>
                {{#  } }}

            </script>
            <!--laytpl 模板 自定义 单元格信息-->
            <script type="text/html" id="titleTpl">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="editRole"><i
                        class="layui-icon layui-icon-edit" shiro:hasPermission="sysRole:edit"></i>配置</a>
            </script>
        </div>
    </div>
</div>
<script type="text/javascript" th:inline="none">
    var table;
    layui.use(['jquery','table'], function () {
        var $ = layui.$;
        table = layui.table;
        var form = layui.form;
        table.render({
            elem: '#table'
            , id: 'table'
            , toolbar: '#user-toolbar'
            , defaultToolbar: []
            , loading: true
            , skin: 'line'
            , method: 'post'
            , url: 'sysRole/listPage' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {type: 'numbers', title: '序号', align: 'center'}
                , {field: 'oid', title: '唯一标识', align: 'center'}
                , {field: 'roleName', title: '角色名', align: 'center'}
                , {field: 'roleSort', title: '排序', align: 'center'}
                , {field: 'createTime', title: '创建时间', align: 'center'}
                , {fixed: 'right', align: 'center', title: '操作', toolbar: '#toolbar'}
                , {fixed: 'right', align: 'center', title: '配置权限', toolbar: '#titleTpl'}
            ]]
        });
        //监听搜索
        form.on('submit(search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {where: field});
            return false;
        });
        //监听行工具事件

        table.on('toolbar(table)', function (obj) {
            if (obj.event === 'add') {
                editLayer({
                    title: '添加角色',
                    url: 'sysRole/edit',
                    area: ['800px', '500px'],
                    back: callback
                });
            }
        });
        table.on('tool(table)', function (obj) {
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            console.log(layEvent)
            if (layEvent === 'del') {
                layer.confirm('真的删除么', function (index) {
                    $.post("sysRole/delBean", {oid: data.oid}, function (res) {
                        if (res.code === 0) {
                            table.reload('table');
                            layer.close(index);
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                    obj.del();
                    //删除对应行（tr）的DOM结构
                });
            } else if (layEvent === 'edit') {
                editLayer({
                    title: '编辑角色',
                    url: 'sysRole/edit?oid=' + data.oid,
                    area: ['800px', '500px'],
                    back: callback
                });
            } else if (layEvent === 'editRole') {
                editLayer({
                    title: '配置权限',
                    url: 'permission/editRole?oid=' + data.oid,
                    area: ['80%', '80%'],
                    back: callbackRole
                });
            }
        });
    });

    function callback(ifr, index, data) {
        var field = data.field;
        $.post("sysRole/saveOrUpdate", field, function (res) {
            if (res.code === 0) {
                topLayer.msg(res.msg);
                topLayer.close(index);
                table.reload('table');
            } else {
                topLayer.msg(res.msg);
            }
        });
    }

    function callbackRole(ifr, index, data) {
        var tree = ifr.treeGrid.checkStatus('treeTable');
        var oid = "";
        for (let i = 0; i < tree.data.length; i++) {
            oid += tree.data[i].oid + ",";
        }
        var fie = {oid: oid.substring(0, oid.length - 1), roleOid: data.field.oid};
        $.post("permission/editUserRole", fie, function (res) {
            if (res.code === 0) {
                topLayer.msg(res.msg);
                topLayer.close(index);
            } else {
                topLayer.msg(res.msg);
            }
        });
    }
</script>
</body>
</html>
